package com.fenqiyong.www;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import java.io.PrintStream;

/**
 * Created by Administrator on 2019/3/13.
 */

public class PhoneFormatUtils implements TextWatcher {
    int afterLen = 0;
    int beforeLen = 0;
    private String beforeString = "";
    private boolean isChange = true;
    private EditText numberEditText;

    public PhoneFormatUtils(EditText paramEditText) {
        this.numberEditText = paramEditText;
        paramEditText.addTextChangedListener(this);
        this.beforeString = paramEditText.getText().toString();
    }

    private String addStringFormat(String paramString) {
        int i = paramString.length();
        this.isChange = false;
        if (i == 1) {
            if (!paramString.startsWith("1"))
                return "";
            return paramString;
        }
        if (i > 0) {
            if (!paramString.startsWith("1"))
                return paramString.substring(1);
            if ((!paramString.startsWith("13")) && (!paramString.startsWith("15")) && (!paramString.startsWith("16")) && (!paramString.startsWith("17")) && (!paramString.startsWith("18"))) {
                if (i > 2) {
                    StringBuilder localStringBuilder3 = new StringBuilder();
                    localStringBuilder3.append("1");
                    localStringBuilder3.append(paramString.substring(2));
                    return localStringBuilder3.toString();
                }
                return "1";
            }
            if (i > 11)
                paramString = paramString.substring(0, 11);
            if (i > 3) {
                StringBuilder localStringBuilder1 = new StringBuilder();
                localStringBuilder1.append(paramString.substring(0, 3));
                localStringBuilder1.append(" ");
                localStringBuilder1.append(paramString.substring(3));
                paramString = localStringBuilder1.toString();
            }
            if (i > 7) {
                StringBuilder localStringBuilder2 = new StringBuilder();
                localStringBuilder2.append(paramString.substring(0, 8));
                localStringBuilder2.append(" ");
                localStringBuilder2.append(paramString.substring(8));
                paramString = localStringBuilder2.toString();
            }
            this.beforeString = paramString;
        }
        return paramString;
    }

    private String subStringFormat(String paramString) {
        int i = paramString.length();
        this.isChange = false;
        if (i == 0)
            return paramString;
        if (i == 1) {
            if (paramString.startsWith("1"))
                return paramString;
            return this.beforeString;
        }
        if ((i >= 2) && (!paramString.startsWith("13")) && (!paramString.startsWith("15")) && (!paramString.startsWith("16")) && (!paramString.startsWith("17")) && (!paramString.startsWith("18")))
            return this.beforeString;
        if (i > 3) {
            StringBuilder localStringBuilder1 = new StringBuilder();
            localStringBuilder1.append(paramString.substring(0, 3));
            localStringBuilder1.append(" ");
            localStringBuilder1.append(paramString.substring(3));
            paramString = localStringBuilder1.toString();
        }
        if (i > 7) {
            StringBuilder localStringBuilder2 = new StringBuilder();
            localStringBuilder2.append(paramString.substring(0, 8));
            localStringBuilder2.append(" ");
            localStringBuilder2.append(paramString.substring(8));
            paramString = localStringBuilder2.toString();
        }
        this.beforeString = paramString;
        return paramString;
    }

    public void afterTextChanged(Editable paramEditable) {
        if (!this.isChange) {
            this.isChange = true;
            return;
        }
        String str1 = this.numberEditText.getText().toString();
        this.afterLen = str1.length();
        if (this.afterLen == 0)
            return;
        String str2 = str1.replace(" ", "");
        if (this.beforeLen > this.afterLen) {
            str2 = subStringFormat(str2);
            PrintStream localPrintStream = System.out;
            StringBuilder localStringBuilder = new StringBuilder();
            localStringBuilder.append(str2);
            localStringBuilder.append("**");
            localPrintStream.println(localStringBuilder.toString());
        } else if (this.beforeLen < this.afterLen) {
            str2 = addStringFormat(str2);
        }
        if (!this.isChange) {
            this.numberEditText.setText(str2);
            if (str2.length() >= 1)
                this.numberEditText.setSelection(str2.length());
        }
    }

    public void beforeTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {
        this.beforeLen = paramCharSequence.length();
    }

    public void onTextChanged(CharSequence paramCharSequence, int paramInt1, int paramInt2, int paramInt3) {
    }
}